﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

namespace Movizard.Model
{
    public enum QueryStatus
    {
        Wait,
        Querying,
        Finished
    }

    public class MovieCollectionItem
    {
        public DirectoryInfo Directory { get; set; }
        public Movie Movie { get; set; }
        public QueryStatus Status { get; set; }
        public event EventHandler QueryFinished;

        public string Year { get { return (Movie == null) ? "" : Movie.Year; } }
        public string Rating { get { return (Movie == null) ? "" : Movie.Rating; } }
        public string Genres { get { return (Movie == null) ? "" : Movie.Genres.ToString(); } }

        public MovieCollectionItem(DirectoryInfo directory)
        {
            this.Directory = directory;
        }

        public void RunQuery()
        {
            if (Directory == null)
                throw new ArgumentException("Directory is null");
            Status = QueryStatus.Querying;
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += worker_DoWork; 
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.RunWorkerAsync(cleanName(Directory.Name));
        }

        public void RenameFolder()
        {
            if (Status == QueryStatus.Finished && Movie != null)
            {
                string newName = Directory.FullName.Substring(0, Directory.FullName.LastIndexOf(Path.DirectorySeparatorChar) + 1) + Movie.Title;
                if (Directory.FullName.ToLower() != newName.ToLower())
                    System.IO.Directory.Move(Directory.FullName, newName);
            }
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Movie = e.Result as Movie;
            Status = QueryStatus.Finished;
            if (QueryFinished != null)
                QueryFinished(this, EventArgs.Empty);
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = Movie.CreateFromIMDB(new IMDB.IMDb(e.Argument.ToString()));
        }

        private string cleanName(string dirtyName)
        {
            string res = dirtyName.ToLower();
            res = res.Replace("dvdrip", " ");
            res = res.Replace("fxg", " ");
            res = res.Replace("[eng]", " ");
            res = res.Replace("[far]", " ");
            res = res.Replace("axxo", " ");
            res = res.Replace("xvid", " ");
            res = res.Replace("divx", " ");
            res = res.Replace("nedivx", " ");
            res = res.Replace("-", " ");
            res = res.Replace("klaxxon", " ");
            res = res.Replace("zektorm", " ");
            res = res.Replace("fxm", " ");
            res = res.Replace("1080p", " ");
            res = res.Replace("_", " ");
            res = res.Replace("noscr", " ");
            res = res.Replace("jns", " ");
            res = res.Replace('.', ' ');
            res = res.Replace('[', ' ');
            res = res.Replace(']', ' ');
            res = res.Replace("(", " ");
            res = res.Replace(")", " ");
            return res;
        }
    }
}
